//
//  LAMerchantPhotoTableView.m
//  LYLazizDevelopDemo
//
//  Created by xbn_iMac on 2016/10/25.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LAMerchantPhotoTableView.h"
#import "LAMerchantPhotoTableViewCell.h"
#import "LZRestaurantPhotoAlbumModel.h"

@interface LAMerchantPhotoTableView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, assign) BOOL isRequesting;

@end

@implementation LAMerchantPhotoTableView

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.separatorColor = [UIColor clearColor];
        self.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
        [self registerNib:[UINib nibWithNibName:@"LAMerchantPhotoTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        self.dataArr = [NSMutableArray array];
        [self createFooterView];
    }
    return self;
}

- (void)checkHaveData {
    if (self.dataArr.count == 0) {
        if (self.subTableDelegate && [self.subTableDelegate respondsToSelector:@selector(requestDateWithType:andPrameter:andPage:andLimit:resultSuccess:)]) {
            
            if (self.isRequesting) {
                return;
            }
            self.isRequesting = YES;
            WS(weakSelf)
            [self.subTableDelegate requestDateWithType:TableViewType_Photo andPrameter:nil andPage:1 andLimit:10 resultSuccess:^(NSArray *result,NSNumber *timeStemp) {
                weakSelf.isRequesting = NO;
                if (result) {
                    // 相片类型（1菜，2环境，3菜单）
                    [weakSelf.dataArr addObjectsFromArray:result];
                    [weakSelf reloadData];
                }
            }];
        }
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LAMerchantPhotoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        // 菜
        cell.labTitle.text = LZLocalizedString(@"restaurant_cuisine", nil);
    }
    else if (indexPath.section == 1) {
        // 环境
        cell.labTitle.text = LZLocalizedString(@"atmosphere", nil);
    }
    else if (indexPath.section == 2) {
        // 菜单
        cell.labTitle.text = LZLocalizedString(@"restaurant_menu", nil);
    }
    
    NSArray *arrTemp = [self.dataArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.type = %ld",indexPath.section+1]];
    if (arrTemp.count>0) {
        cell.model = arrTemp[0];
    }
    else {
        cell.model = nil;
    }

    return cell;

}


#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat photosTotleHight ;
    
    NSArray *arrTemp = [self.dataArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self.type = %ld",indexPath.section+1]];
    if (arrTemp.count>0) {
        LZRestaurantPhotoAlbumModel *model = arrTemp[0];
        photosTotleHight = [LAMerchantPhotoTableViewCell totleHeight:model.photos];
    }
    else {
        photosTotleHight = [LAMerchantPhotoTableViewCell totleHeight:@[]];
    }
    
    return photosTotleHight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 0.01;
    }
    return 10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
